<?php

/**
 * draft actions.
 *
 * @package    sf_sandbox
 * @subpackage draft
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class draftActions extends autodraftActions
{
    public function executeCheck()
    {
        $update = false;
        $draftLast = '';
        $arr = Draft::last($this->getRequestParameter('userId')
                        , $this->getRequestParameter('project')
                        , $this->getRequestParameter('objType')
                        , $this->getRequestParameter('part')
                        , $this->getRequestParameter('objId')
                        , $this->getRequestParameter('lng'));
        if(!empty($arr))
        {
            $draftLast = $arr[0];
        }else 
        {
            $update = true;
        }
        if($draftLast)
        {
            if($draftLast->isChanged($this->getRequestParameter('code')))
            {
                $update = true;
            }
        }else 
        {
            $update = true;
        }
        if($update)
        {
            $this->renderText(json_encode(array("status" => MyConstants::STATUS_APPROVED)));
        }else 
        {
            $this->renderText(json_encode(array("status" => MyConstants::STATUS_FAILED)));
        }
        return sfView::NONE;
    }

    public function executeMake() 
    {
        /* @var $draftLast Draft */
        Draft::make(1
                , $this->getRequestParameter('objType')
                , $this->getRequestParameter('objId')
                , $this->getRequestParameter('code')
                , $this->getRequestParameter('userId')
                , $this->getRequestParameter('part')
                , $this->getRequestParameter('lng')
                , $this->getRequestParameter('content'));
        //returns 10 links with preview
        $this->drafts = Draft::Last( $this->getRequestParameter('userId')
                                    , 1
                                    , $this->getRequestParameter('objType')
                                    , $this->getRequestParameter('part')
                                    , $this->getRequestParameter('objId')
                                    , $this->getRequestParameter('lng')
                                    , 10);
    }

    public function executeRestore() 
    {
        $req = $this->getRequest();
        
        $draft = DraftPeer::retrieveByPk($req->getParameter('id'));
        $draft = $draft->getContent();
        $upclass = ucfirst($req->getParameter('classname'));
        $lowclass = strtolower($req->getParameter('classname'));
        //$obj = call_user_func(array('TextModulePeer', 'retrieveByPk($req->getParameter(\'objid\'))'));
        
        eval('$obj = ' . $upclass . 'Peer::retrieveByPk($req->getParameter(\'objid\'));');
        eval('$obj->set' . ucfirst($req->getParameter('field')) . '($draft);');
        $obj->save();
        
        //debugvar($obj);
        //die();
        
        $this->redirect($lowclass . '/edit?id=' . $req->getParameter('objid'));
        //$this->redirect($req->getParameter('objtype') . '/edit?id=' . $req->getParameter('objid') . '&draft=' . $req->getParameter('id'));
    }

    public function executeShow() 
    {
        $this->draft = DraftPeer::retrieveByPk($this->getRequestParameter('id'));
        $this->redirectIf(empty($this->draft) || ($this->draft->getUserId() != get_user_id()), 'system/401');
    }
}
